

#include "machine_state.h"

namespace assembler {

MachineState::MachineState() {
}

double MachineState::reg(int id) const {
  CheckRegisterIdAllowed(id);
  return reg_[id];
}

void MachineState::set_reg(int id, double value) {
  CheckRegisterIdAllowed(id);
  reg_[id] = value;
}

void MachineState::push(double value) {
  stack_.push(value);
}

void MachineState::pop(int id) {
  CheckRegisterIdAllowed(id);
  if (stack_.empty())
    throw MachineStateStackUnderflowException();
  reg_[id] = stack_.top();
  stack_.pop();
}

void MachineState::CheckRegisterIdAllowed(int id) {
  if (id < 0 || id >= RegisterCount)
    throw MachineStateInvalidRegisterIdException(id);
}

}  // namespace assembler